//
//  KZPronounceReviewListScroller.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/24.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZPronounceReviewListScroller.h"

#import "KZPronounceDetailModel.h"

@implementation KZPronounceReviewListScroller
{
    UIImageView *_emptyView;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.pagingEnabled = true;
        self.bounces = false;
        
        _emptyView = [UIImageView new];
        _emptyView.image = [UIImage imageNamed:@"kz-暂无数据"];
        [self addSubview:_emptyView];
        [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];

    }
    return self;
}

- (void)setSoundmarkList:(NSArray *)soundmarkList {
    
    _soundmarkList = soundmarkList;
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj != _emptyView) {
            [obj removeFromSuperview];
        }
    }];
    
    if (_soundmarkList.count == 0) {
        _emptyView.hidden = false;
        return;
    } else {
        _emptyView.hidden = true;
    }
    
    UIButton *last = nil;
    CGFloat margin = (SCREEN_WIDTH - 100.0 / 750.0 * SCREEN_WIDTH * 5) / 7;
    for (int i = 0; i < _soundmarkList.count; i ++) {
        
        KZPronounceDetailModel *model = _soundmarkList[i];
        
        UIButton *soundmarkView = [UIButton buttonWithType:UIButtonTypeCustom];
        soundmarkView.tag = i;
        soundmarkView.layer.cornerRadius = 2;
        soundmarkView.layer.masksToBounds = true;
        soundmarkView.titleLabel.font = FONT(20);
        soundmarkView.backgroundColor = [[UIColor colorWithHex:0x01131B] colorWithAlphaComponent:0.5];
        [soundmarkView setTitle:model.PhoneticSymbolDes forState:UIControlStateNormal];
        [soundmarkView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [soundmarkView addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:soundmarkView];
        [soundmarkView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(100.0 / 750.0 * SCREEN_WIDTH);
            // 每页的第一排
            if (i % 5 == 0) {
                make.left.mas_equalTo(margin * 1.5 + i / 20 * SCREEN_WIDTH);
                // 每页第一个
                if (i % 20 == 0) {
                    make.top.mas_equalTo(margin);
                } else {
                    make.top.equalTo(last.mas_bottom).offset(margin);
                }
            } else {
                make.left.equalTo(last.mas_right).offset(margin);
                make.top.equalTo(last);
            }
        }];
        
        
        last = soundmarkView;
    }
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    
    
    self.contentSize = CGSizeMake(ceil(_soundmarkList.count / 20.0) * self.width, 0);
}

#pragma mark - 事件
- (void)click:(UIButton *)button {
    
    if (self.clickBlock) {
        self.clickBlock(button.tag);
    }
}

@end
